
import 'package:flutter/material.dart';
import 'package:flutter_application_testview/main.dart';
import 'package:flutter_application_testview/model/heartRate_data.dart';
import 'package:provider/provider.dart';
import '../fitness_app_theme.dart';
import 'package:fl_chart/fl_chart.dart';

class WorkoutView extends StatelessWidget {
  final AnimationController? animationController;
  final Animation<double>? animation;

  const WorkoutView({Key? key, this.animationController, this.animation})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: animationController!,
      builder: (BuildContext context, Widget? child) {
        final heartRateData = Provider.of<HeartRateData>(context); // 获取数据

           return FadeTransition(
          opacity: animation!,
          child: Transform(
            transform: Matrix4.translationValues(
                0.0, 30 * (1.0 - animation!.value), 0.0),
            child: Padding(
              padding: const EdgeInsets.only(
                  left: 24, right: 24, top: 16, bottom: 18),
              child: SizedBox( // 添加固定高度
                height: 280,
                child: Container(
                  decoration: BoxDecoration(
                  gradient: LinearGradient(
                    colors: [
                      const Color.fromARGB(255, 241, 231, 175),
                      HexColor("#6F56E8")
                    ],
                    begin: Alignment.topLeft,
                    end: Alignment.bottomRight,
                  ),
                  borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(8.0),
                    bottomLeft: Radius.circular(8.0),
                    bottomRight: Radius.circular(8.0),
                    topRight: Radius.circular(68.0),
                  ),
                  boxShadow: <BoxShadow>[
                    BoxShadow(
                      color: const Color.fromARGB(255, 36, 22, 226)
                          .withOpacity(0.6),
                      offset: Offset(1.1, 1.1),
                      blurRadius: 10.0,
                    ),
                  ],
                ),
                  child: Padding(
                    padding: const EdgeInsets.all(16.0),
                    child: Column(
                      mainAxisSize: MainAxisSize.min, // 重要！
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                      Text(
                        '实时心率监测',
                        style: TextStyle(
                          fontFamily: FitnessAppTheme.fontName,
                          fontSize: 18,
                          color: FitnessAppTheme.white,
                        ),
                      ),
                      SizedBox(height: 12),
                      Expanded(
                        child: LineChart(
                          LineChartData(
                            gridData: FlGridData(show: false),
                            titlesData: FlTitlesData(
                              bottomTitles: AxisTitles(
                                sideTitles: SideTitles(showTitles: false),
                              ),
                              rightTitles: AxisTitles(
                                sideTitles: SideTitles(showTitles: false),
                              ),
                              topTitles: AxisTitles(
                                sideTitles: SideTitles(showTitles: false),
                              ),
                              leftTitles: AxisTitles(
                                sideTitles: SideTitles(
                                  showTitles: true,
                                  reservedSize: 28,
                                  getTitlesWidget: (value, meta) {
                                    return Text(
                                      '${value.toInt()}',
                                      style: TextStyle(
                                        color: FitnessAppTheme.white,
                                        fontSize: 12,
                                      ),
                                    );
                                  },
                                ),
                              ),
                            ),
                            borderData: FlBorderData(show: false),
                             maxX: heartRateData.heartRate_chart_data.length > 30 
          ? heartRateData.heartRate_chart_data.length.toDouble() - 1 
          : (heartRateData.heartRate_chart_data.isEmpty 
              ? 0 
              : heartRateData.heartRate_chart_data.length.toDouble() - 1),
      lineBarsData: [
        LineChartBarData(
          spots: _getVisibleSpots(heartRateData), // 动态获取可见数据点
                                isCurved: true,
                                color:Colors.red.withOpacity(0.8),
                                barWidth: 2,
                                dotData: FlDotData(show: false),
                                belowBarData: BarAreaData(
                                  show: true,
                                  color: Colors.red.withOpacity(0.2),
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.only(top: 12),
                        child: Text(
                          '更新时间: ${DateTime.now().toString().substring(11, 19)}',
                          style: TextStyle(
                            color: FitnessAppTheme.white.withOpacity(0.8),
                            fontSize: 12,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
              ), 
         
            
            ),
          ),
        );
      },
    );
  }
  // 添加数据截取方法
List<FlSpot> _getVisibleSpots(HeartRateData data) {
  final allSpots = data.heartRate_chart_data
      .asMap()
      .entries
      .map((e) => FlSpot(
          e.key.toDouble(), 
          e.value.value.toDouble()))
      .toList();

  // 显示最近30个点（当数据超过30时）
  if (allSpots.length > 600) {
    return allSpots.sublist(allSpots.length - 600);
  }
  return allSpots;
}
}

